using System;
using System.Collections.Generic;
using Common;

namespace Entity
{
    public sealed partial class EntityManager
    {
        /// <summary>
        /// 实体信息
        /// </summary>
        private sealed class EntityInfo : IReference
        {
            /// <summary>
            /// 实体
            /// </summary>
            private IEntity m_Entity;
            /// <summary>
            /// 实体状态
            /// </summary>
            private EntityStatus m_Status;
            /// <summary>
            /// 父实体
            /// </summary>
            private IEntity m_ParentEntity;
            /// <summary>
            /// 子实体列表
            /// </summary>
            private List<IEntity> m_ChildrenEntities;

            public EntityInfo()
            {
                m_Entity = null;
                m_Status = EntityStatus.Unknown;
                m_ParentEntity = null;
                m_ChildrenEntities = new List<IEntity>();
            }


            public IEntity Entity
            {
                get
                {
                    return m_Entity;
                }
            }

            public EntityStatus Status
            {
                get
                {
                    return m_Status;
                }
                set
                {
                    m_Status = value;
                }
            }


            public IEntity ParentEntity
            {
                get
                {
                    return m_ParentEntity;
                }
                set
                {
                    m_ParentEntity = value;
                }
            }

            public int ChildEntityCount
            {
                get
                {
                    return m_ChildrenEntities.Count;
                }
            }

            public static EntityInfo Create(IEntity entity)
            {
                if (entity == null)
                    throw new Exception("Entity is invalid.");
                EntityInfo entityInfo = ReferencePool.Acquire<EntityInfo>();
                entityInfo.m_Entity = entity;
                entityInfo.m_Status = EntityStatus.WillInit;
                return entityInfo;
            }

            /// <summary>
            /// 返回第一个子实体
            /// </summary>
            /// <returns></returns>
            public IEntity GetChildEntity()
            {
                return m_ChildrenEntities.Count > 0 ? m_ChildrenEntities[0] : null;
            }
            
            /// <summary>
            /// 得到所有子实体
            /// </summary>
            /// <returns></returns>
            public IEntity[] GetChildrenEntities()
            {
                return m_ChildrenEntities.ToArray();
            }

            public void AddChildEntity(IEntity childEntity)
            {
                if(m_ChildrenEntities.Contains(childEntity))
                    throw new Exception("Child entity already exists.");
                m_ChildrenEntities.Add(childEntity);
            }


            public void RemoveChildEntity(IEntity childEntity)
            {
                if (!m_ChildrenEntities.Remove(childEntity))
                {
                    throw new Exception("Child entity does not exist.");
                }
            }

            public void Clear()
            {
                m_Entity = null;
                m_Status = EntityStatus.Unknown;
                m_ParentEntity = null;
                m_ChildrenEntities = null;
            }
        }
    }
}